package com.yd.app.mapper.provider;

import com.alibaba.druid.util.StringUtils;
import com.yd.domain.business.Statistical;

import java.util.Map;

/**
 * 统计分析
 * Created by sunxianhui on 2018/3/20.
 */
public class StatisticalProvider {
    /**
     * 根据产品类型获取每一页列表
     * @param map
     * @return
     */
    public String searchListByType(Map map) {
        Statistical statistical = (Statistical) map.get("statistical");
        StringBuilder sb = new StringBuilder("SELECT SUM(og.dealPrice) totalSales,COUNT(og.dealNum) salesVolume,g.type FROM business_order_goods og left JOIN " +
                " business_order o on o.orderId=og.orderId left join " +
                " business_goods g on g.id = og.goodsId where 1=1 and o.payStatus='1'");
        sb.append(searchOtherSql(statistical));
        sb.append(" GROUP BY g.type limit #{startRow},#{pageSize} ");
        return sb.toString();
    }

    /**
     * 根据产品类型获取查询总数量
     * @param map
     * @return
     */
    public String searchCountByType(Map map) {
        Statistical statistical = (Statistical) map.get("statistical");
        StringBuilder sb = new StringBuilder("select COUNT(1) FROM (select count(1) from business_order_goods og left JOIN " +
                " business_order o on o.orderId=og.orderId left join " +
                " business_goods g on g.id = og.goodsId where 1=1 and o.payStatus='1' ");
        sb.append(searchOtherSql(statistical));
        sb.append(" GROUP BY g.type) a");
        return sb.toString();
    }

    /**
     * 根据客户类型获取每一页列表
     * @param map
     * @return
     */
    public String searchListByUser(Map map) {
        Statistical statistical = (Statistical) map.get("statistical");
        StringBuilder sb = new StringBuilder("SELECT SUM(og.dealPrice) totalSales,COUNT(og.dealNum) salesVolume,u.type FROM business_order_goods og left JOIN " +
                " business_order o on o.orderId=og.orderId left join " +
                " sys_user u on u.id = o.userId where 1=1 and o.payStatus='1'");
        sb.append(searchOtherSql(statistical));
        sb.append(" GROUP BY u.type limit #{startRow},#{pageSize} ");
        return sb.toString();
    }

    /**
     * 根据客户类型获取查询总数量
     * @param map
     * @return
     */
    public String searchCountByUser(Map map) {
        Statistical statistical = (Statistical) map.get("statistical");
        StringBuilder sb = new StringBuilder("select COUNT(1) FROM (select count(1) from business_order_goods og left JOIN " +
                " business_order o on o.orderId=og.orderId left join " +
                " sys_user u on u.id = o.userId where 1=1 and o.payStatus='1' ");
        sb.append(searchOtherSql(statistical));
        sb.append(" GROUP BY u.type) a");
        return sb.toString();
    }
    /**
     * 拼装查询条件
     * @param statistical
     * @return
     */
    public String searchOtherSql(Statistical statistical) {
        StringBuilder sb = new StringBuilder();
//        if(!StringUtils.isEmpty(evaluate.getContent())&&!"".equals(evaluate.getContent())){
//            sb.append(" and content like" +" '%"+evaluate.getContent().trim()+"%'  ");
//        }
//        if(!StringUtils.isEmpty(statistical.getPayStatus())&&!"".equals(statistical.getPayStatus())){
//            sb.append(" and o.expressCode ='"+statistical.getPayStatus()+"'  ");
//        }
        return sb.toString();
    }
}
